package cn.amaake.controller.student;

import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import cn.amaake.common.model.Student;
import cn.amaake.interceptor.StuLoginInterceptor;
import cn.amaake.service.StudentService;
import cn.amaake.service.StudentServiceImp;

@Clear({StuLoginInterceptor.class})
public class StuLoginController extends Controller {

	@Inject(StudentServiceImp.class)
	private StudentService studentService;
	
	public void randomcode(){
        renderCaptcha();
    }

    public void index(){
    	boolean resultcode = validateCaptcha("code");
    	if(resultcode) {
	        String number = getPara("number");
	        String password = getPara("password");
	        Student student = studentService.login(number,password);
	        if(student==null){
	            renderJson(Ret.fail("msg","账号或密码错误，登录失败！"));
	        }else{
	            setSessionAttr("stusession",student);
	            renderJson(Ret.ok("msg","登录成功！"));
	        }
    	}else {
    		renderJson(Ret.fail("msg","验证码错误！"));
    	}
    }

    public void logout(){
        removeSessionAttr("stusession");
        redirect("/");
    }
}
